package com.springboot.activiti.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.activiti.engine.IdentityService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.founder.ssm.core.service.impl.BaseServiceImpl;
import com.springboot.activiti.model.LeaveModel;
import com.springboot.activiti.service.LeaveService;

@Service("leaveService")
public class LeaveServiceImpl extends BaseServiceImpl<LeaveModel> implements LeaveService {

	@Autowired
	private RuntimeService runtimeService;
	
	@Autowired
	private IdentityService identityService;
	
	public LeaveServiceImpl(){
		this.setNamespace("Leave");
	}
	
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void saveAndSubmit(LeaveModel model) {
		
		String leaveId = UUID.randomUUID().toString().replaceAll("-", "");
		model.setLeaveId(leaveId);
		
		Map<String, Object> variables = new HashMap<String, Object>();
		variables.put("processTitle", "员工请假");
		variables.put("businessKey", leaveId);
		//流程启动人的userId
		String initiator = model.getUserId();//从session中获取的当前登录人
		identityService.setAuthenticatedUserId(initiator);
		ProcessInstance processInstance  = runtimeService.startProcessInstanceByKey("LeaveProcess", leaveId, variables);
		
		model.setApplyTime(new Date());
		model.setProcessInstanceId(processInstance.getId());
		this.insert(model);
		
	}

}